/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.actor;
import javax.swing.*;
import java.awt.event.*;
import model.utility.ImageUtility;
import model.game.*;
import model.creature.moving_creature.Player;
import model.creature.moving_creature.Direction;
import model.map.cell.TeleportCell;
import view.main.BackGround;

/**
 *
 * @author Satrio Dewantono
 */
public class PlayerActor extends Actor implements ActionListener {
  private int abs_x,abs_y;
  private int imgidx;
  private int keycode;
  private InformationLayer il_;
  private GameEngine game_;
  private Player controller_;
  private GameController gc_;
  private BackGround bg;
  Timer timer;

    public PlayerActor(GameEngine eng, BackGround bck) {
	x =175;
	y = 100;
	game_ = eng;
	bg = bck;
	il_ = new InformationLayer(game_);
	gc_ = new GameController(game_);
	controller_ = game_.getCreatures().getPlayer();
	abs_x = controller_.getCell().getX();
	abs_y = controller_.getCell().getY();
        setImage(1);
	imgidx = 1;
	keycode = 0;

	timer = new Timer(100,this);
	timer.start();
    }

    public void actionPerformed(ActionEvent e){
      /*if (imgidx<73){
	imgidx++;
      }else{
	imgidx = 1;
      }*/
      switch(keycode){
	case 0: break;
	case 1: if (controller_.getDirection() == Direction.kLeft){
		  animIncrement(5,8);
		}
		break;
	case 2: if (controller_.getDirection() == Direction.kRight){
		  animIncrement(9,12);
		}
		break;
	case 3: if (controller_.getDirection() == Direction.kUp){
		  animIncrement(13,16);
		}
		break;
	case 4: if (controller_.getDirection() == Direction.kDown){
		  animIncrement(1,4);
		}
		break;
      }
    }

    public int getImgIdx(){
      return imgidx;
    }
    
    public void setImage(int x){
      ImageUtility iu = new ImageUtility();
      super.setImage(iu.loadImage("resources/pictures/player/player"+x+".png"));
    }

    public void animIncrement(int start,int stop){
      if ((imgidx < stop)&&(imgidx >= start)){
	      imgidx++;
	      if (imgidx == 24){
		imgidx=25;
	      }
	  }else{
	    imgidx = start;
	  }
    }

    public void buyStuff(String item){
      gc_.playerBuy(item, 1);
    }

    public void hire(String item){
      gc_.playerHireSprite(item);
    }

    public void fire(String item){
      gc_.playerFireSprite(item);
    }

    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();
	//Cell cellinfront = controller_.getCellInFrontOffSelf();

        if (key == KeyEvent.VK_LEFT) {
	  if (controller_.getDirection() != Direction.kLeft){
	    controller_.setDirection(Direction.kLeft);
	    setImage(5);
	  }
	  keycode = 1;
        }

        if (key == KeyEvent.VK_RIGHT) {
	  if (controller_.getDirection() != Direction.kRight){
	    controller_.setDirection(Direction.kRight);
	    setImage(9);
	  }
	  keycode = 2;
        }

        if (key == KeyEvent.VK_UP) {
	  if (controller_.getDirection() != Direction.kUp){
	    controller_.setDirection(Direction.kUp);
	    setImage(13);
	  }
	  keycode = 3;
        }

        if (key == KeyEvent.VK_DOWN) {
	  if (controller_.getDirection() != Direction.kDown){
	    controller_.setDirection(Direction.kDown);
	    setImage(1);
	  }
	  keycode = 4;
        }
    }

    public void keyTyped(KeyEvent e){
      int key = e.getKeyCode();

      if (key == KeyEvent.VK_W){
	gc_.playerPerformToolAction();
      }else
      if (key == KeyEvent.VK_Q){
	gc_.showPreviousTool();
      }else
      if (key == KeyEvent.VK_E){
	gc_.showNextTool();
      }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

	keycode = 0;
        if (key == KeyEvent.VK_LEFT) {
           setImage(5);
        }else

        if (key == KeyEvent.VK_RIGHT) {
            setImage(9);
        }else

        if (key == KeyEvent.VK_UP) {
            setImage(13);
        }else

        if (key == KeyEvent.VK_DOWN) {
            setImage(1);
        }else

	if (key == KeyEvent.VK_W){
	  gc_.playerPerformToolAction();
	  bg.makeDrawList();
	}else
	if (key == KeyEvent.VK_Q){
	  gc_.showPreviousTool();
	}else
	if (key == KeyEvent.VK_E){
	  gc_.showNextTool();
	}else

	if (key == KeyEvent.VK_T){
	  if (il_.getCellAt(controller_.getCell().getX(),controller_.getCell().getY()).isTeleport()){
	    TeleportCell tc = (TeleportCell)controller_.getCell();
	    gc_.playerTeleport(tc.getTeleportDestination());
	  }
	}else

	if (key == KeyEvent.VK_S){
	  gc_.playerPlant();
	  //bg.makeDrawList();
	}else
	if (key == KeyEvent.VK_A){
	  gc_.showPreviousInventory();
	}else
	if (key == KeyEvent.VK_D){
	  gc_.showNextInventory();
	}else

	if (key == KeyEvent.VK_C){
	  gc_.playerSell(1);
	  //bg.makeDrawList();
	}else

	if (key == KeyEvent.VK_R){
	  gc_.playerSleep();
	  //bg.makeDrawList();
	}else

	if (key == KeyEvent.VK_Y){
	  gc_.playerSave();
	  //bg.makeDrawList();
	}
    }
}
